package org.lex.swing;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;

import javax.swing.Icon;

import org.lex.utils.Codec;


public class ColorIcon implements Icon {
	public static final int DEFAULT_WIDTH = 16;
	public static final int DEFAULT_HEIGHT = 16;
	private int height = 0;
	private int width = 0;
	private int arcWidth = -1;
	private int arcHeight = -1;
	private Color color = null;

	public ColorIcon(Color color) {
		this(color, DEFAULT_WIDTH, DEFAULT_HEIGHT);
	}

	public ColorIcon(int width, int height) {
		this(null, width, height);
	}

	public ColorIcon(Color color, int width, int height) {
		this(color, width, height, -1, -1);
	}

	public ColorIcon(Color color, int width, int height, int round) {
		this(color, width, height, round, round);
	}

	public ColorIcon(Color color, int width, int height, int arcWidth, int arcHeight) {
		super();
		this.set(color, width, height, arcWidth, arcHeight);
	}

	private void set(Color color, int width, int height, int arcWidth, int arcHeight) {
		this.color = color;
		this.width = width;
		this.height = height;
		this.arcWidth = arcWidth;
		this.arcHeight = arcHeight;
	}

	public Color getColor() {
		return this.color;
	}

	public int getIconHeight() {
		return height;
	}

	public int getIconWidth() {
		return width;
	}

	public void paintIcon(Component c, Graphics g, int x, int y) {
		if (null == color)
			return;
		g.setColor(color);
		if (Math.min(arcWidth, arcWidth) < 0)
			g.fillRect(x, y, width, height);
		else
			g.fillRoundRect(x, y, width, height, arcWidth, arcHeight);
	}

	public String toString() {
		StringBuilder buf = new StringBuilder();
		buf.append(this.getClass().getCanonicalName());
		buf.append("[width=").append(this.width);
		buf.append(", height=").append(this.height);
		buf.append(", color=").append(Codec.encodeColor(this.color));
		buf.append(']');
		return buf.toString();
	}
}
